package com.springboot.cloud.demos.ribbon.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.core.exception.SystemErrorType;
import com.springboot.cloud.demos.entity.form.AlbumForm;
import com.springboot.cloud.demos.entity.form.MomentForm;
import com.springboot.cloud.demos.entity.po.Album;
import com.springboot.cloud.demos.entity.po.Moment;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MomentService {

    @Autowired
    RestTemplate restTemplate;

    @SentinelResource(fallback = "saveFallback")
    public Result<Moment> save(@RequestBody MomentForm momentForm) {
        Result<Moment> result = restTemplate.postForEntity("http://producer/moment", momentForm, Result.class).getBody();
        if (result.isSuccess()) {
            Map map = (Map<String, Object>) result.getData();
            String urls = momentForm.getUrls();
            if (StringUtils.isNotEmpty(urls)) {
                String urlArray[] = urls.split(";");
                List<Album> albums = new ArrayList<>();
                for (String url : urlArray) {
                    Album album = new Album();
                    album.setUrl(url);
                    album.setUserId(momentForm.getUserId());
                    album.setType(momentForm.getType());
                    album.setVoiceLength(momentForm.getVoiceLength());
                    album.setMomentId(map.get("id").toString());
                    albums.add(album);
                }
                restTemplate.postForEntity("http://producer/album/batchAdd", albums, Result.class).getBody();
            }
        }
        return result;
    }

    public Result saveFallback(@RequestBody MomentForm momentForm) {
        return Result.fail(SystemErrorType.SYSTEM_BUSY);
    }

    @SentinelResource(fallback = "getFallback")
    public Result<Moment> get(@PathVariable String id) {
        return restTemplate.getForEntity("http://producer/moment/{1}", Result.class, id).getBody();
    }

    @SentinelResource(fallback = "deleteFallback")
    public Result delete(@PathVariable String id) {
        restTemplate.delete("http://producer/moment/" + id);
        return Result.success();
    }

    @SentinelResource(fallback = "getFallback")
    public Result list(@PathVariable String userId, Integer type, Integer pageNo, Integer pageSize) {
        return restTemplate.getForEntity("http://producer/moment/list/{1}?type={2}&pageNo={3}&pageSize={4}", Result.class, userId, type, pageNo, pageSize).getBody();
    }

    @SentinelResource(fallback = "getFallback")
    public Result followList(String userId, Integer sex, Integer real, Integer pretty, Integer pageNo, Integer pageSize) {
        return restTemplate.getForEntity("http://producer/moment/followList?userId={1}&type={2}&pageNo={3}&pageSize={4}", Result.class, userId, sex, real, pretty, pageNo, pageSize).getBody();
    }

    @SentinelResource(fallback = "getFallback")
    public Result recommendList(String userId, Integer sex, Integer real, Integer pretty, Integer pageNo, Integer pageSize) {
        return restTemplate.getForEntity("http://producer/moment/recommendList?userId={1}&sex={2}&real={3}&pretty={4}&pageNo={5}&pageSize={6}", Result.class, userId, sex, real, pretty, pageNo, pageSize).getBody();
    }

    @SentinelResource(fallback = "getFallback")
    public Result broadcastList(Integer sex, Integer real, Integer pretty, Integer type, Integer pageNo, Integer pageSize) {
        return restTemplate.getForEntity("http://producer/moment/broadcastList?sex={1}&real={2}&pretty={3}&type={4}&pageNo={5}&pageSize={6}", Result.class, sex, real, pretty, type, pageNo, pageSize).getBody();
    }

    @SentinelResource(fallback = "getFallback")
    public Result nearbyList(Integer sex, Integer real, Integer pretty, float longitude, float latitude, Integer pageNo, Integer pageSize) {
        return restTemplate.getForEntity("http://producer/moment/nearbyList?sex={1}&real={2}&pretty={3}&longitude={4}&latitude={5}&pageNo={6}&pageSize={7}", Result.class, sex, real, pretty, longitude, latitude, pageNo, pageSize).getBody();
    }

    public Result getFallback() {
        return Result.fail(SystemErrorType.SYSTEM_BUSY);
    }

    public Result deleteFallback() {
        return Result.fail(SystemErrorType.SYSTEM_BUSY);
    }

    public Result getMomentMap(String id) {
        return restTemplate.getForEntity("http://producer/moment/getMomentMap?id={1}", Result.class, id).getBody();
    }

    public Result next(String momentId) {
        return restTemplate.getForEntity("http://producer/moment/next?momentId={1}", Result.class, momentId).getBody();
    }

    public Result prev(String momentId) {
        return restTemplate.getForEntity("http://producer/moment/prev?momentId={1}", Result.class, momentId).getBody();
    }
}
